/**
 * Created by maxiao on 2018/3/14.
 */

// import {CookieUtil} from './CookieUtil.js'
import {ConstJsonManager} from './ConstJsonManager'
import {mutations_const} from "../config/StoreConstant";

var NetUtil = {
    success: '200'
}, axios = require('axios')
var apiUrl = 'http://localhost:8080/api/'
// apiUrl = 'https://xiaoma.ittun.com/api/'
// apiUrl = 'https://writepen.capsui.com/hongchen-api/'
// apiUrl = 'https://api.xuxinzy.top/'
// apiUrl = 'https://writepen1.ittun.com/hongchen-api/'
apiUrl = 'http://192.168.1.237:8080/hongchen-api/'
apiUrl = 'https://sygbackstage2.hongchentech.com/'
// apiUrl = 'http://192.168.1.237:7000'
// apiUrl = 'http://writepen1.ittun.com/hongchen-api'

NetUtil.updToken = () => {
    return new Promise(resolve => {
        ConstJsonManager.get('token')
            .then(res => {
                axios.defaults.headers['Authorization'] = res
                resolve()
            })
    })
}

axios.defaults.baseURL = apiUrl

NetUtil.isAlert = false
NetUtil.post = (url, params) => {
    return axios.post(url, params)
        .then((res) => {
            return new Promise(function (resolve, reject) {
                if (res.data.code == NetUtil.success) {
                    var data = res.data.data
                    resolve(data)
                } else {
                    reject(res)
                }
            })
        })
        .catch((res) => {
            NetUtil.errTips(res)
        })
}

NetUtil.postToken = (url, params) => {
    return axios.post(url, params)
        .then((res) => {

        })
}

NetUtil.postText = (url, text) => {
    return axios.post(url, text, {
        headers: {
            'Content-type': 'text/plain'
        }
    })
        .then(() => {
            return new Promise(function (resolve, reject) {
                var data = res.data.data;
                resolve(data)
            })
        })
}
NetUtil.postpageDataAll = (url, params) => {
    params.size = 999
    params.index = 1
    return axios.post(url, params)
        .then(res => {
            return new Promise(function (resolve, reject) {
                resolve(res.data.records)
            })
        })
}

NetUtil.postPage = (() => {
    var index = 1
    var size = 10

    return (url, params, page) => {
        if (page.size) {
            size = page.size
        }
        if (page.index) {
            index = page.index
        }

        if (page.loading) {
            return new Promise((resolve, reject) => {
                reject(' 正在请求，请稍后再试。。。。。。。', 'error')
            })
        }
        page.loading = true
        params.index = index
        params.size = size
        return axios.post(url, params)
            .then((res) => {
                // console.info(res)
                page.loading = false
                page.index = ++index

                page.size = res.data.size
                page.current = res.data.current
                page.total = res.data.total
                page.pages = Math.ceil(res.data.total / size)

                return new Promise(function (resolve, reject) {
                    resolve(res.data.records)
                })
            })
            .catch((res) => {
                page.loading = false
                NetUtil.errTips(res)
            })
    }
})()

// 避免同时出现多个错误弹窗
NetUtil.errTips = (res) => {
    if (!NetUtil.isAlert) {
        NetUtil.isAlert = true;
        let msg = '';
        let code = '';
        if (res.response) {
            if (res.response.data) {
                msg = res.response.data.message || '未知错误';
                code = res.response.data.code;
            } else if (res.response.message) {
                msg = res.response.message || '未知错误';
                code = res.response.code;
            }
        } else if (res.data) {
            msg = res.data.message || '未知错误';
            code = res.data.code;
        } else if (res.message) {
            msg = res.message || '未知错误';
            code = res.code;
        } else {
            msg = '未知错误';
            code = '0000';
        }
        vueMain.$alert(msg, '错误提示', {showCancelButton: false, showClose: false})
            .then(() => {
                NetUtil.isAlert = false
                if (code == '8001' || code == '8000' || msg == 'token无效' || msg == 'Token无效' || msg == 'TOKEN无效' || (msg.indexOf('登录') !== -1) || (msg.indexOf('登陆') !== -1)) {
                    vueMain.$store.commit(mutations_const.RESET_LOGIN_MEMBER, {});
                    vueMain.$store.commit(mutations_const.RESET_CLASS, {
                        info: {},
                        member: {},
                        teachingAssistant: {},
                        device: {
                            groupList: [],
                            list: []
                        },
                    });
                    Promise.all([
                        ConstJsonManager.set('token', ''),
                        ConstJsonManager.set('rememberPass', false)
                    ])
                        .then(() => {
                            vueMain.$router.push('/login')
                        })
                }
            })
    }
};

export {NetUtil}
